'use strict';

const Service = require('egg').Service;

class CollectService extends Service {
    //添加收藏
    async create(w_id,uid) {
        await this.app.mysql.query('insert into collectlist (uid,w_id) values (?,?)',[uid,w_id])
    }
    //取消收藏
    async destroy(id){
        await this.app.mysql.query('delete from collectlist where id=?',[id])
    }
    //查询收藏文档的列表
    async index(uid){
        let wordList = await this.app.mysql.query('select * from wordlist where id in (select c.w_id from collectlist c where uid=?)',[uid])
        for(let i = 0;i<wordList.length;i++){
            let {uid,s_id} = wordList[i];
            let user = await this.app.mysql.query('select u.username from userlist u where id=?',[uid])
            let store = await this.app.mysql.query('select s.title from storelist s where id=?',[s_id])
            wordList[i].username = user[0].username;
            wordList[i].storeTitle = store[0].title;
        }
        return wordList
    }
}

module.exports = CollectService;
